package com.maket.jbxx.service.impl;

import java.util.List;

import com.maket.jbxx.domain.CpAndSx;
import com.maket.jbxx.domain.SxAttributeValue;
import com.maket.jbxx.domain.vo.CpProductType;
import com.maket.jbxx.mapper.CpAndSxMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.maket.jbxx.domain.SxProductAttribute;
import com.maket.jbxx.mapper.CpProductMapper;
import com.maket.jbxx.domain.CpProduct;
import com.maket.jbxx.service.ICpProductService;

/**
 * 产品管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-04
 */
@Service
public class CpProductServiceImpl implements ICpProductService 
{
    @Autowired
    private CpProductMapper cpProductMapper;

    @Autowired
    private CpAndSxMapper cpAndSxMapper;

    /**
     * 查询产品管理
     * 
     * @param cpCode 产品管理主键
     * @return 产品管理
     */
    @Override
    public CpProduct selectCpProductByCpCode(String cpCode)
    {
        return cpProductMapper.selectCpProductByCpCode(cpCode);
    }

    /**
     * 查询产品管理列表
     * 
     * @param cpProduct 产品管理
     * @return 产品管理
     */
    @Override
    public List<CpProduct> selectCpProductList(CpProduct cpProduct)
    {
        return cpProductMapper.selectCpProductList(cpProduct);
    }

    @Override
    public List<CpProduct> selectCpProductType(CpProduct cpProduct) {

        return cpProductMapper.selectCpProductType(cpProduct);
    }

    @Override
    public List<CpProduct> selectCpProductTypeCode(CpProduct cpProduct) {
        return cpProductMapper.selectCpProductTypeCode(cpProduct);
    }

    /**
     * 新增产品管理
     * 
     * @param cpProduct 产品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertCpProduct(CpProduct cpProduct)
    {
        List<CpAndSx> list = new ArrayList<>();


        for (CpProductType attributeValue : cpProduct.getSxAttributeValueList()) {
            // 创建 CpAndSx 实例
            CpAndSx cpAndSx = new CpAndSx();

            cpAndSx.setCpCode(cpProduct.getCpCode());
            cpAndSx.setSxCode(attributeValue.getSxCode());
            cpAndSx.setSxZId(attributeValue.getzId());

            // 添加到 CpAndSx 列表
            list.add(cpAndSx);
        }

        cpAndSxMapper.batchInsertCpAndSx(list);

        return cpProductMapper.insertCpProduct(cpProduct);
    }

    /**
     * 修改产品管理
     * 
     * @param cpProduct 产品管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateCpProduct(CpProduct cpProduct)
    {
        List<CpAndSx> list = new ArrayList<>();
        // 创建 CpAndSx 实例
        for (CpProductType attributeValue : cpProduct.getSxAttributeValueList()) {
            for (CpProductType cpAndSxList : attributeValue.getSxAttributeValueList()){
                CpAndSx cpAndSx = new CpAndSx();
                cpAndSx.setCpCode(cpProduct.getCpCode());
                cpAndSx.setSxCode(cpAndSxList.getSxCode());
                cpAndSx.setSxZId(attributeValue.getzId());
                cpAndSx.setCpSxId(cpAndSxList.getCpSxId());

                cpAndSxMapper.updateCpAndSx(cpAndSx);
            }
        }


        return cpProductMapper.updateCpProduct(cpProduct);
    }

    @Override
    public int updateCpppProductBrandIsStatus(CpProduct cpProduct) {
        return cpProductMapper.updateCpppProductBrandIsStatus(cpProduct);
    }

    /**
     * 批量删除产品管理
     * 
     * @param cpCodes 需要删除的产品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCpProductByCpCodes(String[] cpCodes)
    {
        return cpProductMapper.deleteCpProductByCpCodes(cpCodes);
    }

    /**
     * 删除产品管理信息
     * 
     * @param cpCode 产品管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteCpProductByCpCode(String cpCode)
    {
        return cpProductMapper.deleteCpProductByCpCode(cpCode);
    }

}
